import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wu_you/data/repository/user_repository.dart';
import 'package:wu_you/l10n/messages.dart';
import 'package:wu_you/model/user/user_model.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/state/bloc/http_bloc.dart';
import 'package:wu_you/state/bloc/user_bloc.dart';
import 'package:wu_you/utils/utils.dart';
import 'package:wu_you/widgets/http_list_widget.dart';
import 'package:wu_you/utils/extension_utils.dart';

///提现记录
class WithdrawRecordPage extends StatefulWidget{
  final String num;
  WithdrawRecordPage({Key key,this.num}):super(key: key);
  @override
  WithdrawRecordPageState createState() => WithdrawRecordPageState();
}
class WithdrawRecordPageState extends State<WithdrawRecordPage>{

  @override
  void initState() {
    super.initState();
  }
  var message;
  @override
  Widget build(BuildContext context) {
    message = AppLocalizations.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("${message['text45']}"),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: 40,
            width: double.infinity,
            padding: EdgeInsets.only(left: 15),
            alignment: Alignment.centerLeft,
            margin: EdgeInsets.only(left: 12,right: 12,top: 12),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(6),
                topRight: Radius.circular(6)
              ),
              color: Colors.white,
            ),
            child: Text("${message['text57']}¥${widget.num}",style: TextStyle(color: AppColors.textColor,fontWeight: FontWeight.bold),),
          ),
          Expanded(
            child: HttpListWidget<WithdrawRecord, WithdrawRecordBloc>(
                creator: (context) => WithdrawRecordBloc(),
                onObtainData: (context, page) {
                  BlocProvider.of<WithdrawRecordBloc>(context).add(PageSizeHttpEvent(page,10));
                },
                successBuilder: (context, data) {
                  return ListView.builder(
                    itemBuilder: (context,index){
                      WithdrawRecord item = data[index];
                      return Container(
                          margin: EdgeInsets.only(left: 12,right: 12),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(5)),
                            color: Colors.white,
                          ),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      _leadIcon(item),
                                      SizedBox(width: 8,),
                                      Column(
                                        mainAxisSize: MainAxisSize.min,
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Row(
                                            mainAxisSize: MainAxisSize.min,
                                            children: [
                                              Text("${message['text58']}",style: TextStyle(fontSize: 15,color: Colors.black,fontWeight: FontWeight.bold),),
                                              _typeName(item),
                                            ],
                                          ),
                                          SizedBox(height: 2,),
                                          Text("${item.time}",style: TextStyle(fontSize: 14,color: AppColors.textHintColor),),
                                        ],
                                      ),
                                    ],
                                  ),
                                  Column(
                                    mainAxisSize: MainAxisSize.min,
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    children: [
                                      Text("¥${item.money}",style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold),),

                                      ///1 待审核  2 成功 -1驳回
                                      Text(_statusText(item),style: TextStyle(color: _statusColor(item)),),
                                    ],
                                  ),
                                ],
                              ).addPadding(EdgeInsets.only(left: 15,right: 15,top: 15)),
                              Offstage(
                                  offstage: item.status == '-1' && item.cause != null ? false: true,
                                  child: Column(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      Text("${item.cause}",style: TextStyle(fontSize: 14,color: AppColors.primaryColor),),
                                      SizedBox(height: 6,)
                                    ],
                                  ).addPadding(EdgeInsets.only(right: 15,left: 15))
                              ),
                              SizedBox(height: 12,),
                              Divider(),
                            ],
                          )
                      );
                    },
                    itemCount: data.length,
                  );
                }
            )
          )
        ],
      )
    );
  }

  Color _statusColor(WithdrawRecord item ){
    Color color;
    if(item.status == "1"){
      color = Colors.yellow;
    }else if(item.status == "2"){
      color = Colors.green;
    }else if(item.status == "-1"){
      color = AppColors.primaryColor;
    }
    return color;
  }

  String _statusText(WithdrawRecord item){
    String status;
    if(item.status == "1"){
      status = "${message['text59']}";
    }else if(item.status == "2"){
      status = "${message['text60']}";
    }else if(item.status == "-1"){
      status = "${message['text61']}";
    }
    return status;
  }

  Widget _leadIcon(WithdrawRecord item){
    Widget child;
    if(item.type == "1"){
      child = Image.asset(Utils.getImgPath('payment_one'),width: 40,height: 40,);
    }else if(item.type == "2"){
      child = Image.asset(Utils.getImgPath('payment_two'),width: 40,height: 40,);
    }else if(item.type == "3"){
      child = Image.asset(Utils.getImgPath('payment_three'),width: 40,height: 40,);
    }else if(item.type == "4"){
      child = Image.asset(Utils.getImgPath('payment_four'),width: 40,height: 40,);
    }else if(item.type == "5"){
      child = Image.asset(Utils.getImgPath('payment_five'),width: 40,height: 40,);
    }
    return child;
  }


  Widget _typeName(WithdrawRecord item){
    Widget child;
    if(item.type == "1"){
      child = Text("${message['text22']}",style: TextStyle(fontSize: 15,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }else if(item.type == "2"){
      child = Text("${message['text23']}",style: TextStyle(fontSize: 15,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }else if(item.type == "3"){
      child = Text("${message['text24']}",style: TextStyle(fontSize: 15,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }else if(item.type == "4"){
      child = Text("${message['text26']}",style: TextStyle(fontSize: 15,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }else if(item.type == "5"){
      child = Text("${message['text30']}",style: TextStyle(fontSize: 15,color: AppColors.textColor,fontWeight: FontWeight.bold),);
    }
    return child;
  }
}